package com.qiyu.admin.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.qiyu.admin.system.mapper.UserMapper;
import com.qiyu.admin.system.model.User;
import com.qiyu.admin.system.service.UserService;
import com.qiyu.framework.base.BaseMapper;
import com.qiyu.framework.base.BaseServiceImpl;
import com.qiyu.framework.util.BeanUtil;
import com.qiyu.framework.util.FastDFSClientUtils;
import com.qiyu.framework.util.PagedResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 用户接口
 *
 * @author zhangqing
 * @date 2016年08月30日
 */
@Service(value="userService")
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Resource(name = "userMapper")
    private UserMapper userMapper;
    @Override
    public BaseMapper getMapper() {
        return userMapper;
    }

    @Override
    public User selectByLoginName(String loginName) throws Exception {
        return userMapper.selectByLoginName(loginName);
    }


    @Override
    public PagedResult<User> selectPageList(Integer pageNo, Integer pageSize, User user) throws Exception {
        pageNo = pageNo == null?1:pageNo;
        pageSize = pageSize == null?10:pageSize;
        PageHelper.startPage(pageNo,pageSize);  //startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
        return BeanUtil.toPagedResult(userMapper.selectListByUser(user));
    }

    @Override
    public boolean isExitLoginName(String loginName) throws Exception {
        int result = userMapper.isExitLoginName(loginName);
        if(result==0){
            return true;
        }
        return false;
    }


    @Override
    public String uploadFile(byte[] file_buff, String file_ext_name) throws Exception{
        FastDFSClientUtils client = new FastDFSClientUtils("classpath:config/client.conf");
        String uploadFile = client.uploadFile(file_buff, file_ext_name, null);
        return uploadFile;
    }

}
